import {
  getBreadCrumbList,
  setTagNavListToLocalstorage,
  getTagNavListFromLocalstorage,
  getHomeRouter,
  getNextRouter,
  routeHasExist,
  routeEqual,
  getRouteTitleHandled,
  getLocal,
  setLocal,
  setSession,
  getSession,
} from '@/utils/navigation'

import router from '@/router'
import * as appConfig from '@/app.config'
const { homeRouterName } = appConfig

export default {
  state: {
    isCollapse: getLocal('isCollapse'),
    breadCrumbList: [],
    tagNavList: [],
    homeRoute: {},
    currentTagName: '',
    local: getLocal('local'),
    errorList: [],
    hasReadErrorPage: false,
    themeColor: getLocal('dintTheme'),
    menuWidth: getLocal('menuWidth'),
    isLockHeader: false, // 是否锁定顶部导航栏
    isShowHeader: true, // 是否现实header
    environment: 'us', // 系统环境us 美东，de欧洲
    logoutUrl: '',
    infoLinkSwitch: getSession('infoLinkSwitch'), // 系统信息条开关状态
    loading: true, // 未开放功能loading开关
    permission: getSession('permission'), // 页面权限
  },
  getters: {
    errorCount: (state) => state.errorList.length,
  },
  mutations: {
    setPermission(state, data) {
      state.permission = data
      setSession('permission', data)
    },
    setEnvironment(state, data) {
      state.logoutUrl = `http://${window.location.host}/test/Logout`
      state.environment = data
    },
    setCollapse(state, data) {
      state.isCollapse = data
      setLocal('isCollapse', data)
    },
    setIsLockHeader(state, data) {
      state.isLockHeader = data
    },
    setIsShowHeader(state, data) {
      state.isShowHeader = data
    },
    setTheme(state, color) {
      state.themeColor = color
      setLocal('dintTheme', color)
    },
    setMenuWidth(state, data) {
      state.menuWidth = data
      setLocal('menuWidth', data)
    },
    setInfoSwitch(state, data) {
      state.infoLinkSwitch = data
      setSession('infoLinkSwitch', data)
    },
    setBreadCrumb(state, route) {
      state.breadCrumbList = getBreadCrumbList(route, state.homeRoute)
    },
    setHomeRoute(state, routes) {
      state.homeRoute = getHomeRouter(routes, homeRouterName)
    },
    setTagNavList(state, list) {
      let tagList = []
      if (list) {
        tagList = [...list]
      } else tagList = getTagNavListFromLocalstorage() || []
      if (tagList[0] && tagList[0].name !== homeRouterName) tagList.shift()
      let homeTagIndex = tagList.findIndex(
        (item) => item.name === homeRouterName
      )
      if (homeTagIndex > 0) {
        let homeTag = tagList.splice(homeTagIndex, 1)[0]
        tagList.unshift(homeTag)
      }
      state.tagNavList = tagList
      setTagNavListToLocalstorage([...tagList])
    },
    closeTag(state, route) {
      let tag = state.tagNavList.filter((item) => routeEqual(item, route))
      route = tag[0] ? tag[0] : null
      if (!route) return
      const nextRouter = getNextRouter(state.tagNavList, route)
      state.tagNavList = state.tagNavList.filter((item) => {
        return !routeEqual(item, route)
      })
      router.push(nextRouter)
    },
    addTag(state, { route, type = 'unshift' }) {
      let router = getRouteTitleHandled(route)
      if (!routeHasExist(state.tagNavList, router)) {
        if (type === 'push') {
          state.tagNavList.push(router)
        } else {
          if (router.name === homeRouterName) state.tagNavList.unshift(router)
          else state.tagNavList.splice(1, 0, router)
        }
        setTagNavListToLocalstorage([...state.tagNavList])
      }
      state.currentTagName = router.name
    },
    setLocal(state, lang) {
      setLocal('local', lang)
      state.local = lang
    },
    addError(state, error) {
      state.errorList.push(error)
    },
    setHasReadErrorLoggerStatus(state, status = true) {
      state.hasReadErrorPage = status
    },
  },
  actions: {},
}
